package com.tfswx.jcyw.sqlmodel.api;

import java.util.ArrayList;

import com.tfswx.jcyw.sqlmodel.api.bean.Node;
import com.tfswx.jcyw.sqlmodel.api.bean.table.Select;
import com.tfswx.jcyw.sqlmodel.api.function.RuntimeFuntion;
import lombok.Data;

import java.util.List;

/**
 * @author 张述江
 * @date 2023/9/13
 */
@Data
public final class OperatorChain {
    private List<Operator> operatorList;
    private List<RuntimeFuntion> functionList;
    private int idx = -1;

    Select invoke(Node node, List<Select> pResultList) {
        idx++;
        if (operatorList.size() <= idx) {
            return null;
        } else {
            return operatorList.get(idx).invoke(node, pResultList, this);
        }
    }

    public Select execute(Node node, List<Select> pResultList) {
        OperatorChain clone = new OperatorChain();
        clone.setOperatorList(operatorList);
        clone.setFunctionList(functionList);
        clone.setIdx(-1);
        return clone.invoke(node, pResultList);
    }
}
